package com.suke.czx.modules.performance.template;

import com.suke.czx.common.utils.ExportTypeEnum;
import com.suke.czx.common.utils.ListUtil;
import com.suke.czx.modules.performance.entity.Performance;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 应用模块名称: 绩效
 * 代码描述: 绩效导出模板
 * copyright: AUZAR ALL RIGHTS RESERVED
 * company: AUZAR
 *
 * @author: 颜米晨
 * @date: 2021/10/20 0:41
 */
@Slf4j
@Service
@AllArgsConstructor
public class PerformanceTemplate {

    /**
     * 创建单公斤明细导出对象
     * @param dataList 单公斤明细对象集合
     * @return 导出对象
     */
    public SXSSFWorkbook createPerformanceExcel(List<Performance> dataList) {
        SXSSFWorkbook workBook = null;
        try {
            //内存中保留100000条数据，以免内存溢出，其余写入硬盘
            workBook = new SXSSFWorkbook(100000);
            //创建sheet
            Sheet sheet = workBook.createSheet();
            workBook.setSheetName(0, ExportTypeEnum.PERFORMANCE.getStatement());
            //标题行
            Row titleRow = sheet.createRow(0);
            createTitleRow(titleRow);
            if(!ListUtil.isEmpty(dataList)){
                int index = 1;
                for(Performance p : dataList){
                    int cellCount = 0;
                    Row row = sheet.createRow(index);
                    // 0.序号
                    row.createCell(cellCount ++).setCellValue(index ++);
                    // 1.绩效名称
                    row.createCell(cellCount ++).setCellValue(p.getPerformanceName());
                    // 2.绩效类型
                    row.createCell(cellCount ++).setCellValue(p.getTypeName());
                    // 3.任务名称
                    row.createCell(cellCount ++).setCellValue(p.getTaskName());
                    // 4.绩效任务分数
                    row.createCell(cellCount ++).setCellValue(p.getPerformanceCategory());
                    // 5.员工姓名
                    row.createCell(cellCount ++).setCellValue(p.getName());
                    // 6.依据
                    row.createCell(cellCount ++).setCellValue(p.getFact());
                    // 7.备注
                    row.createCell(cellCount ++).setCellValue(p.getNote());
                    // 8.创建时间
                    row.createCell(cellCount).setCellValue(p.getCreateTime());
                }
            }
        } catch (Exception e) {
            log.error("create performance workbook error::", e);
        }
        return workBook;
    }

    /**
     * 为下载的excel文件创建表头内容
     * @param titleRow 标题行
     */
    private void createTitleRow(Row titleRow) {
        String[] titleArr = new String[]{"序号", "绩效名称", "绩效类型", "任务名称", "绩效任务分数", "员工姓名", "依据", "备注", "创建时间"};
        for (int i = 0; i < titleArr.length; i ++) {
            titleRow.createCell(i).setCellValue(titleArr[i]);
        }
    }

}
